#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <string.h>
#include <dir.h>
#include <stdlib.h>
#include <math.h>
#include "defs.h"
#include "kernel.h"
#include "setup.h"
#include "utils.h"
#include "gcode.h"
#include "file.h"
#include "control.h"
#include "help.h"
#include "operator.h"

int version;
int main(int argc, char *argv[])
{
   float cur_speed=0, dist_x=0, dist_y=0, dist_z=0, dist_a=0, tfloat;
   double num, num2, loopmax;
   int msg=COMPLETE, i, ch, ch2, kp=0, tint, gosub=OFF, goloop=OFF, loopnum;
   unsigned long slow_freq, fast_freq;
   long place_holder;
   static char drive[MAXDRIVE], dir[MAXDIR], tstring[20];
   static char fstring[200];
   static BYTE buffer[1000], buffer2[1500], buffer3[100], buffer4[400], buffer5[1000];
   static struct hard_setup hardware;
   static struct soft_setup software, temp_software;
   static struct gcode_setup gcode;
   static struct control ctrl;
   static struct filepos sub[100];
   static struct pos pt, world;
   FILE *ifp, *ifp2;
   fpos_t fpos1, fpos2, fpos3, fpostemp, subpos, mainpos;
   BYTE s_port_read;

   //get, store, and set execution path of program
   if(argc>0)
    {fnsplit(argv[0],drive,dir,NULL,NULL);
     if(strlen(dir)>0) dir[strlen(dir)-1]=0;
     system(drive); chdir(dir);
    }

   version=FULL;

   //splash screen
   clrscr();
   _setcursortype(_NOCURSOR);
   box(20,4,60,19, LIGHTGREEN,WHITE,WHITE,WHITE);
   gotoxy(4,4);  cprintf("                                   ");
   gotoxy(4,5);  cprintf("                                   ");
   gotoxy(4,6);  cprintf("           CNCPro V4.2             ");
   gotoxy(4,7);  cprintf("                                   ");
   gotoxy(4,8);  cprintf("    orginal code by Doug Yeager    ");
   gotoxy(4,9);  cprintf("                                   ");
   gotoxy(4,10); cprintf("   this version tweaked by Alan    ");
   gotoxy(4,11); cprintf("                                   ");
   gotoxy(4,12); cprintf("       Compiled: 2008-03-26        ");
   gotoxy(4,13); cprintf("                                   ");

   //pause 1 second
   sleep(1);

   //init. control screen variables
   world.x_pos=world.y_pos=world.z_pos=world.a_pos=
   pt.x_pos=pt.y_pos=pt.z_pos=pt.a_pos=0;
   ctrl.jog_type=FAST; ctrl.jog_inc=0; ctrl.frate=0;
   sprintf(ctrl.file,"NONE");
   ctrl.cs=0; ctrl.pty=ABS;
   ctrl.M00=ctrl.M03=ctrl.M07=ctrl.M08=ctrl.M10=ctrl.cvv=OFF;
   load_screen_setup(&world,&pt,&ctrl);

   //define hardware and software data structures
   define_data(&hardware, &software);

   //guage computer
   begin_realtime(&software, 10000);
   slow_freq=guage(&software);
//	slow_freq=10000;
   fast_freq=slow_freq*2;
   end_realtime();

   //begin realtime control
   load_setup(&hardware, &software, "<DEFAULT>");
   load_setup(&hardware, &software, "SETUP.INI");
   if(software.update_after==ON) begin_realtime(&software, fast_freq);
   else begin_realtime(&software, slow_freq);
   load_setup(&hardware, &software, "<DEFAULT>");
   load_setup(&hardware, &software, "SETUP.INI");
   for(i=0;i<maxtoolsplusone;i++) sprintf(gcode.tool_dis[i],"NONE");
   load_gcode_setup(&gcode);

   //reassign port bits
   clear_data_bits(&hardware);
   if(hardware.io1_on==HIGH) clear_io_bit(&hardware, hardware.io1_bit);
   else set_io_bit(&hardware, hardware.io1_bit);
   if(hardware.io2_on==HIGH) clear_io_bit(&hardware, hardware.io2_bit);
   else set_io_bit(&hardware, hardware.io2_bit);
   if(hardware.io3_on==HIGH) clear_io_bit(&hardware, hardware.io3_bit);
   else set_io_bit(&hardware, hardware.io3_bit);
   if(hardware.io4_on==HIGH) clear_io_bit(&hardware, hardware.io4_bit);
   else set_io_bit(&hardware, hardware.io4_bit);

   //print control screen
   control_screen(&software, &pt, &ctrl, &gcode);
   for(i=0; i<20; i++) if(kbhit()) getch();
   gettext(43,17,78,17,buffer3);

   while(kp!=27)
     {for(i=0;i<20;i++) if(kbhit()) getch();
      kp=getch();
      if(kp==0) kp=getch()+1000; cur_speed=0;
      switch(kp)
        {case 35+1000:  //alt h
         case 59+1000:  //F1
            help(1);
            break;
         case 'v':
         case 'V':

            if(ctrl.cvv==ON) ctrl.cvv=OFF;
            else ctrl.cvv=ON;
            break;
         case 63+1000:  //F5
            if(software.e_stop!=OFF) s_port_read=(248&inportb(hardware.port+1))^128;
            if(software.e_stop!=OFF && hardware.e_trip*hardware.e_bit-(s_port_read&hardware.e_bit)==0) {msg=E_STOP; break;}
            if(ctrl.M03==OFF)
              {if(hardware.io1_on==HIGH) set_io_bit(&hardware, hardware.io1_bit);
               else clear_io_bit(&hardware, hardware.io1_bit);
               ctrl.M03=ON;}
            else
              {if(hardware.io1_on==HIGH) clear_io_bit(&hardware, hardware.io1_bit);
               else set_io_bit(&hardware, hardware.io1_bit);
               ctrl.M03=OFF;}
            break;
         case 64+1000:  //F6
            if(software.e_stop!=OFF) s_port_read=(248&inportb(hardware.port+1))^128;
            if(software.e_stop!=OFF && hardware.e_trip*hardware.e_bit-(s_port_read&hardware.e_bit)==0) {msg=E_STOP; break;}
            if(ctrl.M07==OFF)
              {if(hardware.io2_on==HIGH) set_io_bit(&hardware, hardware.io2_bit);
               else clear_io_bit(&hardware, hardware.io2_bit);
               ctrl.M07=ON;}
            else
              {if(hardware.io2_on==HIGH) clear_io_bit(&hardware, hardware.io2_bit);
               else set_io_bit(&hardware, hardware.io2_bit);
               ctrl.M07=OFF;}
            break;
         case 65+1000:  //F7
            if(software.e_stop!=OFF) s_port_read=(248&inportb(hardware.port+1))^128;
            if(software.e_stop!=OFF && hardware.e_trip*hardware.e_bit-(s_port_read&hardware.e_bit)==0) {msg=E_STOP; break;}
            if(ctrl.M08==OFF)
              {if(hardware.io3_on==HIGH) set_io_bit(&hardware, hardware.io3_bit);
               else clear_io_bit(&hardware, hardware.io3_bit);
               ctrl.M08=ON;}
            else
              {if(hardware.io3_on==HIGH) clear_io_bit(&hardware, hardware.io3_bit);
               else set_io_bit(&hardware, hardware.io3_bit);
               ctrl.M08=OFF;}
            break;
         case 66+1000:  //F8
            if(software.e_stop!=OFF) s_port_read=(248&inportb(hardware.port+1))^128;
            if(software.e_stop!=OFF && hardware.e_trip*hardware.e_bit-(s_port_read&hardware.e_bit)==0) {msg=E_STOP; break;}
            if(ctrl.M10==OFF)
              {if(hardware.io4_on==HIGH) set_io_bit(&hardware, hardware.io4_bit);
               else clear_io_bit(&hardware, hardware.io4_bit);
               ctrl.M10=ON;}
            else
              {if(hardware.io4_on==HIGH) clear_io_bit(&hardware, hardware.io4_bit);
               else set_io_bit(&hardware, hardware.io4_bit);
               ctrl.M10=OFF;}
            break;
         case 'o':
         case 'O':
            tint=ctrl.cs; ctrl.cs++;
            if(ctrl.cs==7) ctrl.cs=0;
            pt.x_pos=pt.x_pos+(gcode.x_coord[tint]-gcode.x_coord[ctrl.cs])*ctrl.pty;
            pt.y_pos=pt.y_pos+(gcode.y_coord[tint]-gcode.y_coord[ctrl.cs])*ctrl.pty;
            pt.z_pos=pt.z_pos+(gcode.z_coord[tint]-gcode.z_coord[ctrl.cs])*ctrl.pty;
            pt.a_pos=pt.a_pos+(gcode.a_coord[tint]-gcode.a_coord[ctrl.cs])*ctrl.pty;
            break;
         case 'u':
         case 'U':
            if(software.units==MM) {ctrl.frate=change_unit(&software, ctrl.frate, IN);
                                 change_pt_units(&software, &world, IN, IN);
                                 change_gcode_units(&software, &gcode, IN, IN);
                                 change_units(&hardware, &software, IN, IN);}
            else {ctrl.frate=change_unit(&software, ctrl.frate, MM);
                  change_pt_units(&software, &world, MM, MM);
                  change_gcode_units(&software, &gcode, MM, MM);
                  change_units(&hardware, &software, MM, MM);}
            break;

			case 1019:		// Alt R

            gettext(20,16,57,20,buffer);
            box(20,16,57,20, BLUE,BLACK,BLACK,WHITE);
            gotoxy(13,1); cprintf("Movement Type");
            gotoxy(8,3); cprintf("(A)bsolute or (R)elative");
            _setcursortype(_NOCURSOR);

            while((i=getch())!=27)
            {
                  if(i=='R' || i=='r') {pt.x_pos=pt.y_pos=pt.z_pos=pt.a_pos=0;ctrl.pty=REL;
                  						   break;
			  									   }
                  if(i=='A' || i=='a') {pt.x_pos=world.x_pos-gcode.x_coord[ctrl.cs]; pt.y_pos=world.y_pos-gcode.y_coord[ctrl.cs];
               pt.z_pos=world.z_pos-gcode.z_coord[ctrl.cs]; pt.a_pos=world.a_pos-gcode.a_coord[ctrl.cs];
               ctrl.pty=ABS; break;
													}
            }
            puttext(20,16,57,20,buffer);
            break;

         case 'w':
         case 'W':
            if(software.a_status==OFF) break;
            if(software.a_follow==INDEPENDANT) software.a_follow=FOLLOW_X;
            else if(software.a_follow==FOLLOW_X) software.a_follow=FOLLOW_Y;
            else if(software.a_follow==FOLLOW_Y) software.a_follow=FOLLOW_Z;
            else if(software.a_follow==FOLLOW_Z) software.a_follow=INDEPENDANT;
            break;
         case 'f':
         case 'F':
            gettext(22,19,59,24,buffer);
            box(22,19,59,24, BLUE,BLACK,WHITE,WHITE);
            gotoxy(12,1); cprintf("<Esc> To Abort");
            gotoxy(10,3); cprintf("Enter New Feed Rate:");
            _setcursortype(_NORMALCURSOR);
            tfloat=0;
            gotoxy(16,4); get_value(&tfloat);
            if(tfloat>9999) {message("F/R Must Be <=9999."); puttext(22,19,59,24,buffer); break;}
            if(software.time==MIN) ctrl.frate=tfloat/60;
            else ctrl.frate=tfloat;
            _setcursortype(_NOCURSOR);
            puttext(22,19,59,24,buffer);
            break;
         case 't':
         case 'T':
            gettext(22,19,59,24,buffer);
            box(22,19,59,24, BLUE,BLACK,WHITE,WHITE);
            gotoxy(13,1); cprintf("<Esc> To Exit");
            gotoxy(10,3); gotoxy(3,8); cprintf("Enter New Tool Number:");
            _setcursortype(_NORMALCURSOR);
            gotoxy(18,4); tint=tfloat=0; get_value(&tfloat); tint=tfloat;
            _setcursortype(_NOCURSOR);
            puttext(22,19,59,24,buffer);
            if(tint<=gcode.tnum_max)
              {pt.z_pos=(pt.z_pos-(gcode.tool_len[tint]-gcode.tool_len[gcode.tnum]))*ctrl.pty;
               gcode.tnum=tint;
              }
            else
              {gettext(22,19,59,24,buffer);
               box(22,19,59,24, RED,BLACK,WHITE,WHITE);
               gotoxy(4,3); cprintf("      Tool Number Not Valid");
               gotoxy(4,4); cprintf("            <Any Key>");
               getch();
               puttext(22,19,59,24,buffer);
              }
            break;

	      case 'j':
         case 'J':
                if (ctrl.jog_type==FAST) ctrl.jog_type=SLOW;
           else if (ctrl.jog_type==SLOW) ctrl.jog_type=FAST;
           else if (ctrl.jog_type==INC) ctrl.jog_type=SLOW;

           break;

        	case 1036:  // Alt-J
            gettext(22,17,59,21,buffer);
            box(22,17,59,21, BLUE,BLACK,BLACK,WHITE);
            gotoxy(13,1); cprintf("Jog Type");
            gotoxy(6,3); cprintf("(F)ast (S)low (I)ncremental");
            _setcursortype(_NOCURSOR);

            while((i=getch())!=27)
            {
                  if(i=='F' || i=='f') {ctrl.jog_type=FAST ;break;}
                  if(i=='S' || i=='s') {ctrl.jog_type=SLOW ;break;}
                  if(i=='I' || i=='i') {ctrl.jog_type=INC  ;break;}
            }
            puttext(22,17,59,21,buffer);
            break;

         case 'i':
         case 'I':
            gettext(22,19,59,24,buffer);
            box(22,19,59,24, BLUE,BLACK,WHITE,WHITE);
            gotoxy(13,1); cprintf("<Esc> To Exit");
            gotoxy(7,3); cprintf("Enter New Jog Increment:");
            _setcursortype(_NORMALCURSOR);
            gotoxy(16,4); get_value(&ctrl.jog_inc);
            _setcursortype(_NOCURSOR);
            puttext(22,19,59,24,buffer);
            break;
         case 71+1000: //home
            if(software.limit==OFF)
              {message("Limit Switches Must Be On To Home.");
               break;
              }
            gettext(22,19,59,24,buffer);
            box(22,19,59,24, RED,BLACK,WHITE,WHITE);
            gotoxy(2,1); cprintf("<Esc> To Exit    <Enter> To Home All");
            gotoxy(4,3); cprintf("<%c>->Home %c",software.x_label, software.x_label);
            gotoxy(21,3); cprintf("<%c>->Home %c",software.y_label, software.y_label);
            gotoxy(4,4); cprintf("<%c>->Home %c",software.z_label, software.z_label);
            if(software.a_status==ON) {gotoxy(21,4); cprintf("<%c>->Home %c",software.a_label, software.a_label);}
            gettext(2,6,22,12,buffer4);
            while((i=getch())!=27)
              {if(i==software.x_label || i==software.x_label+32)
                 {move_menu(1); if((msg=homing(&hardware, &software, &world, X_AXIS, NULL, NULL, NULL))!=COMPLETE) break;}
               if(i==software.y_label || i==software.y_label+32)
                 {move_menu(1); if((msg=homing(&hardware, &software, &world, Y_AXIS, NULL, NULL, NULL))!=COMPLETE) break;}
               if(i==software.z_label || i==software.z_label+32)
                 {move_menu(1); if((msg=homing(&hardware, &software, &world, Z_AXIS, NULL, NULL, NULL))!=COMPLETE) break;}
               if((i==software.a_label || i==software.a_label+32) && software.a_status==ON)
                 {move_menu(1); if((msg=homing(&hardware, &software, &world, A_AXIS, NULL, NULL, NULL))!=COMPLETE) break;}
               if(i==13)
                 {move_menu(1);
                  if((msg=homing(&hardware, &software, &world, Z_AXIS, POS, A_AXIS, POS))!=COMPLETE) break;
                  if((msg=homing(&hardware, &software, &world, X_AXIS, NULL, Y_AXIS, NULL))!=COMPLETE) break;
                  if(software.home_z==NEG && software.home_a==NEG)
                     if((msg=homing(&hardware, &software, &world, Z_AXIS, NULL, A_AXIS, NULL))!=COMPLETE) break;
                  if(software.home_z==NEG && software.home_a==POS)
                     if((msg=homing(&hardware, &software, &world, Z_AXIS, NULL, NULL, NULL))!=COMPLETE) break;
                  if(software.home_a==NEG && software.home_z==POS)
                     if((msg=homing(&hardware, &software, &world, A_AXIS, NULL, NULL, NULL))!=COMPLETE) break;
                  break;
                 }
               puttext(2,6,22,12,buffer4);
               pt.x_pos=(world.x_pos-gcode.x_coord[ctrl.cs])*ctrl.pty;
               pt.y_pos=(world.y_pos-gcode.y_coord[ctrl.cs])*ctrl.pty;
               pt.z_pos=(world.z_pos-gcode.z_coord[ctrl.cs])*ctrl.pty;
               pt.a_pos=(world.a_pos-gcode.a_coord[ctrl.cs])*ctrl.pty;
               update_display(&software, pt.x_pos, pt.y_pos, pt.z_pos, pt.a_pos, 0, 0, ctrl.frate, ALL);
              }
            puttext(2,6,22,12,buffer4);
            puttext(22,19,59,24,buffer);
            break;
         case 31+1000:     //alt s
            temp_software=software;
            end_realtime();
            setup(&hardware, &software);

            //reset for new freq if neccessary
            load_setup(&hardware, &software, software.file);
            if(software.update_after==ON) begin_realtime(&software, fast_freq);
            else begin_realtime(&software, slow_freq);
            load_setup(&hardware, &software, software.file);

            change_units(&hardware, &software, software.units, software.units);
            ctrl.frate=change_unit(&software, ctrl.frate, software.units);
            change_pt_units(&temp_software, &world, software.units, software.a_units);
            change_gcode_units(&temp_software, &gcode, software.units, software.a_units);
            save_setup(&hardware, &software, "SETUP.INI");
            break;
         case 33+1000:     //alt f
            if(ctrl.M00==ON) {do_gcode(&hardware, &software, &gcode, &ctrl, &pt, 'M', 30);
                              ctrl.M00=OFF; message("");}
            if(strcmp(ctrl.file,"NONE")!=0) fclose(ifp);
            temp_software=software;
            end_realtime();

				if ((get_file(ctrl.file, software.file)) == MAYBEQUIT)
	            {msg=MAYBEQUIT; goto case_27;}

            //reset for new freq if neccessary
            load_setup(&hardware, &software, software.file);
            if(software.update_after==ON) begin_realtime(&software, fast_freq);
            else begin_realtime(&software, slow_freq);
            load_setup(&hardware, &software, software.file);

            change_units(&hardware, &software, software.units, software.units);
            ctrl.frate=change_unit(&software, ctrl.frate, software.units);
            change_pt_units(&temp_software, &world, software.units, software.a_units);
            change_gcode_units(&temp_software, &gcode, software.units, software.a_units);
            save_setup(&hardware, &software, "SETUP.INI");

            window(43,3,78,18); textcolor(WHITE); textbackground(BLACK); clrscr();
            if(strcmp(ctrl.file,"NONE")==0) {window(1,1,80,25); break;}
            sprintf(tstring,"GCODE\\%s",ctrl.file);
            if((ifp=fopen(tstring,"r"))!=NULL)
              {//go to after M30
               while((ch=fgetc(ifp))!=EOF)
                 {if(ch=='(')
                    {while((ch=fgetc(ifp))!=EOF && ch!='\n');
                     if(ch==EOF) break;
                    }
                  if(ch=='M' || ch=='m')
                    {if(NULL==fscanf(ifp, "%lf", &num)) continue;
                     if(num==30) break;
                    }
                 }
               //clear old subs from memory and get new subs id's and file positions
               for(i=0;i<100;i++) sub[i].id=-1;
               while(ch!=EOF && (ch=fgetc(ifp))!=EOF)
                 {if(ch=='(')
                    {while((ch=fgetc(ifp))!=EOF && ch!='\n');
                     if(ch==EOF) break;
                    }
                  if(ch=='$')
                    {if(NULL==fscanf(ifp, "%lf", &num)) continue;
                     if(num>=0) for(i=0;i<100;i++) if(sub[i].id==-1) {sub[i].id=num; fgetpos(ifp,&sub[i].pos); break;}
                    }
                 }
               //reset file
               rewind(ifp);
               //clear all variables
               get_var(0,0,CLEAR);
               //set up file positions
               fgetpos(ifp,&fpos1);
               for(i=0;i<8;i++)
                 {if(fgets(fstring, 200, ifp)!=NULL)
                    {fstring[36]=0; textcolor(WHITE); textbackground(BLACK); gotoxy(1,8+i); cprintf("%s",fstring);}
                 }
               fgetpos(ifp,&fpos2); fsetpos(ifp, &fpos1);
              }
            else sprintf(ctrl.file,"NONE");
            window(1,1,80,25);
            break;
         case 24+1000:    //alt o
            temp_software=software;
            operator_setup(&hardware, &software, &gcode);
            ctrl.frate=change_unit(&software, ctrl.frate, software.units);
            change_pt_units(&temp_software, &world, software.units, software.a_units);
            save_gcode_setup(&gcode);
            break;

// ---------------------------------------------------------------------------------------------------------------

//         case 72+1000:     //up
//         case 77+1000:     //right
//            if((kp==72+1000 && software.invert_arrows==OFF) || (kp==77+1000 && software.invert_arrows==ON))
//               msg=jog(&hardware, &software, &pt, ctrl.jog_type, ctrl.jog_inc, Y_AXIS, POS);
//            else msg=jog(&hardware, &software, &pt, ctrl.jog_type, ctrl.jog_inc, X_AXIS, POS);
//            break;

//         case 80+1000:    //down
//         case 75+1000:    //left
//            if((kp==80+1000 && software.invert_arrows==OFF) || (kp==75+1000 && software.invert_arrows==ON))
//               msg=jog(&hardware, &software, &pt, ctrl.jog_type, ctrl.jog_inc, Y_AXIS, NEG);
//            else msg=jog(&hardware, &software, &pt, ctrl.jog_type, ctrl.jog_inc, X_AXIS, NEG);
//            break;

         case 72+1000:     //up
         	      	  if(software.invert_arrows==0 || software.invert_arrows==2 ) msg=jog(&hardware, &software, &pt, ctrl.jog_type, ctrl.jog_inc, Y_AXIS, NEG);
         	      	  if(software.invert_arrows==1 || software.invert_arrows==3 ) msg=jog(&hardware, &software, &pt, ctrl.jog_type, ctrl.jog_inc, Y_AXIS, POS);
         	      	  if(software.invert_arrows==4 || software.invert_arrows==6 ) msg=jog(&hardware, &software, &pt, ctrl.jog_type, ctrl.jog_inc, X_AXIS, NEG);
         	      	  if(software.invert_arrows==5 || software.invert_arrows==7 ) msg=jog(&hardware, &software, &pt, ctrl.jog_type, ctrl.jog_inc, X_AXIS, POS);
                        break;

         case 80+1000:    //down

         	      	  if(software.invert_arrows==0 || software.invert_arrows==2 ) msg=jog(&hardware, &software, &pt, ctrl.jog_type, ctrl.jog_inc, Y_AXIS, POS);
         	      	  if(software.invert_arrows==1 || software.invert_arrows==3 ) msg=jog(&hardware, &software, &pt, ctrl.jog_type, ctrl.jog_inc, Y_AXIS, NEG);
         	      	  if(software.invert_arrows==4 || software.invert_arrows==6 ) msg=jog(&hardware, &software, &pt, ctrl.jog_type, ctrl.jog_inc, X_AXIS, POS);
         	      	  if(software.invert_arrows==5 || software.invert_arrows==7 ) msg=jog(&hardware, &software, &pt, ctrl.jog_type, ctrl.jog_inc, X_AXIS, NEG);
								break;

         case 77+1000:     //right
                       if(software.invert_arrows==0 || software.invert_arrows==1 ) msg=jog(&hardware, &software, &pt, ctrl.jog_type, ctrl.jog_inc, X_AXIS, POS);
         	      	  if(software.invert_arrows==2 || software.invert_arrows==3 ) msg=jog(&hardware, &software, &pt, ctrl.jog_type, ctrl.jog_inc, X_AXIS, NEG);
         	      	  if(software.invert_arrows==4 || software.invert_arrows==5 ) msg=jog(&hardware, &software, &pt, ctrl.jog_type, ctrl.jog_inc, Y_AXIS, POS);
         	      	  if(software.invert_arrows==6 || software.invert_arrows==7 ) msg=jog(&hardware, &software, &pt, ctrl.jog_type, ctrl.jog_inc, Y_AXIS, NEG);
								break;
         case 75+1000:    //left
                       if(software.invert_arrows==0 || software.invert_arrows==1 ) msg=jog(&hardware, &software, &pt, ctrl.jog_type, ctrl.jog_inc, X_AXIS, NEG);
         	      	  if(software.invert_arrows==2 || software.invert_arrows==3 ) msg=jog(&hardware, &software, &pt, ctrl.jog_type, ctrl.jog_inc, X_AXIS, POS);
         	      	  if(software.invert_arrows==4 || software.invert_arrows==5 ) msg=jog(&hardware, &software, &pt, ctrl.jog_type, ctrl.jog_inc, Y_AXIS, NEG);
         	      	  if(software.invert_arrows==6 || software.invert_arrows==7 ) msg=jog(&hardware, &software, &pt, ctrl.jog_type, ctrl.jog_inc, Y_AXIS, POS);
								break;



// ---------------------------------------------------------------------------------------------------------------------

         case 73+1000:   //page up
            msg=jog(&hardware, &software, &pt, ctrl.jog_type, ctrl.jog_inc, Z_AXIS, POS);
            break;
         case 81+1000:   //page down
            msg=jog(&hardware, &software, &pt, ctrl.jog_type, ctrl.jog_inc, Z_AXIS, NEG);
            break;
         case '+':
         case '=':
            msg=jog(&hardware, &software, &pt, ctrl.jog_type, ctrl.jog_inc, A_AXIS, POS);
            break;
         case '-':
            msg=jog(&hardware, &software, &pt, ctrl.jog_type, ctrl.jog_inc, A_AXIS, NEG);
            break;
         case 'z':
         case 'Z':
            if(ctrl.cs!=0)
              {
              {gettext(20,19,61,23,buffer);
               box(20,19,61,23,RED,BLACK,WHITE,WHITE);
               gotoxy(4,3); cprintf("Must be in MCS/G53 to adjust an axis");
              }
              getch();
              puttext(20,19,61,23,buffer);
            break;
            }

            else

            gettext(22,19,59,24,buffer);
            box(22,19,59,24, RED,BLACK,WHITE,WHITE);
            gotoxy(2,1); cprintf("<Esc> To Exit   <Enter> To Zero All");
            gotoxy(4,3); cprintf("<%c> -> Adjust %c",software.x_label, software.x_label);
            gotoxy(21,3); cprintf("<%c> -> Adjust %c",software.y_label, software.y_label);
            gotoxy(4,4); cprintf("<%c> -> Adjust %c",software.z_label, software.z_label);
            if(software.a_status==ON) {gotoxy(21,4); cprintf("<%c> -> Adjust %c",software.a_label, software.a_label);}
            while((i=getch())!=27)
              {if(i>96 && i<123) i=i-32;
               if(i==software.x_label || i==software.y_label || i==software.z_label || (i==software.a_label && software.a_status==ON))
                 {_setcursortype(_NORMALCURSOR);
                  gotoxy(6,6); cprintf("Enter New '%c' Value:",i); gotoxy(27,6);
                  if(i==software.x_label) {tfloat=pt.x_pos; get_value_all(&tfloat); pt.x_pos=tfloat;}
                  if(i==software.y_label) {tfloat=pt.y_pos; get_value_all(&tfloat); pt.y_pos=tfloat;}
                  if(i==software.z_label) {tfloat=pt.z_pos; get_value_all(&tfloat); pt.z_pos=tfloat;}
                  if(i==software.a_label) {tfloat=pt.a_pos; get_value_all(&tfloat); pt.a_pos=tfloat;}
                  gotoxy(6,6); cprintf("                             "); _setcursortype(_NOCURSOR);
                 }
               if(i==13) {pt.x_pos=pt.y_pos=pt.z_pos=pt.a_pos=0; break;}
               update_display(&software, pt.x_pos, pt.y_pos, pt.z_pos, pt.a_pos, 0, 0, ctrl.frate, ALL);
              }
            puttext(22,19,59,24,buffer);
            break;
         case 'm':
         case 'M':
            if(ctrl.frate<.00009) {message("F/R Must Be >0 To Move."); break;}
            gettext(22,19,59,24,buffer);
            box(22,19,59,24, BLUE,BLACK,WHITE,WHITE);
            gotoxy(2,1); cprintf("<Esc> To Exit        <Enter> To Move");
            gotoxy(4,3); cprintf("<%c> -> %s",software.x_label, prn_val_all(dist_x));
            gotoxy(21,3); cprintf("<%c> -> %s",software.y_label, prn_val_all(dist_y));
            gotoxy(4,4); cprintf("<%c> -> %s",software.z_label, prn_val_all(dist_z));
            if(software.a_status==ON) {gotoxy(21,4); cprintf("<%c> -> %s",software.a_label, prn_val_all(dist_a));}
            while((i=getch())!=27 && i!=13)
              {if(i>96 && i<123) i=i-32;
               if(i==software.x_label || i==software.y_label || i==software.z_label || (i==software.a_label && software.a_status==ON))
                 {_setcursortype(_NORMALCURSOR);
                  gotoxy(6,6); cprintf("Enter New '%c' Value:",i); gotoxy(27,6);
                  if(i==software.x_label) {get_value_all(&dist_x); gotoxy(4,3); cprintf("<%c> -> %s",i,prn_val_all(dist_x));}
                  if(i==software.y_label) {get_value_all(&dist_y); gotoxy(21,3); cprintf("<%c> -> %s",i,prn_val_all(dist_y));}
                  if(i==software.z_label) {get_value_all(&dist_z); gotoxy(4,4); cprintf("<%c> -> %s",i,prn_val_all(dist_z));}
                  if(i==software.a_label) {get_value_all(&dist_a); gotoxy(21,4); cprintf("<%c> -> %s",i,prn_val_all(dist_a));}
                  gotoxy(6,6); cprintf("                             "); _setcursortype(_NOCURSOR);
                 }
              }
            puttext(22,19,59,24,buffer);
            if(i==27) break;
            gettext(2,6,22,12,buffer4);
            move_menu(2);
            message("Machine Move In Progress.");
            msg=move(&hardware, &software, &pt, &cur_speed, &ctrl.frate,
                     dist_x-(world.x_pos-gcode.x_coord[ctrl.cs])*ctrl.pty,
                     dist_y-(world.y_pos-gcode.y_coord[ctrl.cs])*ctrl.pty,
                     dist_z-(world.z_pos-gcode.z_coord[ctrl.cs])*ctrl.pty,
                     dist_a-(world.a_pos-gcode.a_coord[ctrl.cs])*ctrl.pty,
                     NULL,LINE_ENT,0,0,0,0,NULL,LINE_ENT);
            message("");
            puttext(2,6,22,12,buffer4);
            break;
         case 'p':
         case 'P':
            if(strcmp(ctrl.file,"NONE")==0)
              {gettext(22,19,59,24,buffer);
               box(22,19,59,24, RED,BLACK,WHITE,WHITE);
               gotoxy(4,3); cprintf("        No Program Loaded");
               gotoxy(4,4); cprintf("            <Any Key>");
               getch();
               window(1,1,80,25);
               puttext(22,19,59,24,buffer);
               break;
              }
            gettext(22,19,59,24,buffer);
            box(22,19,59,24, BLUE,BLACK,WHITE,WHITE);
            gotoxy(5,1); cprintf("Program Control <Esc> To Exit");
            gotoxy(4,3); cprintf("<%c> Skip Forward   <%c> Skip Back",25,24);
            gotoxy(4,4); cprintf("<R> Rewind And Reset Program");
            window(1,1,80,25);
            while(1)
              {while((ch=getch())!=27 && ch!=80 && ch!=72 && ch!='r' && ch!='R');
               if(ch==27) break;
               if(ch==80)
                 {while((ch2=fgetc(ifp))!='\n' && ch2!=EOF);
                  if(ch2==EOF) {message("End Of Program"); fsetpos(ifp,&fpos1); continue;}
                  gettext(43,5,78,17,buffer2); puttext(43,4,78,16,buffer2); puttext(43,17,78,17,buffer3);
                  fgetpos(ifp,&fpos1); fsetpos(ifp,&fpos2);
                  if(fgets(fstring, 200, ifp)!=NULL)
                    {fstring[36]=0; gotoxy(43,17); textcolor(WHITE); textbackground(BLACK); cprintf("%s",fstring);}
                  fgetpos(ifp,&fpos2); fsetpos(ifp,&fpos1);
                  message("Skipped Forward");
                  goloop=OFF;
                 }
               if(ch==72)
                 {place_holder=ftell(ifp);
                  rewind(ifp);
                  if(ftell(ifp)==place_holder) {message("Beginning Of Program"); continue;}
                  fgetpos(ifp,&fpos3); i=0;
                  while(ftell(ifp)!=place_holder)
                    {fgetpos(ifp,&fpos1);
                     if(i<=6) i++;
                     if(i==7) {fsetpos(ifp,&fpos3); fgets(fstring, 200, ifp); fgetpos(ifp,&fpos3); fsetpos(ifp,&fpos1);}
                     while((ch2=fgetc(ifp))!='\n' && ch2!=EOF && ftell(ifp)!=place_holder);
                     if(ch2==EOF) break;
                    }
                  gettext(43,4,78,16,buffer2); puttext(43,5,78,17,buffer2); puttext(43,4,78,4,buffer3);
                  if(i==7) {fstring[36]=0; gotoxy(43,4); textcolor(WHITE); textbackground(BLACK); cprintf("%s",fstring);}
                  fsetpos(ifp,&fpos1);
                  for(i=0;i<8;i++) fgets(fstring, 200, ifp);
                  fgetpos(ifp,&fpos2); fsetpos(ifp,&fpos1);
                  message("Skipped Back");
                  goloop=OFF;
                 }
               if(ch=='r' || ch=='R')
                 {do_gcode(&hardware, &software, &gcode, &ctrl, &pt, 'M', 30);
                  window(43,3,78,18); textcolor(WHITE); textbackground(BLACK); clrscr();
                  rewind(ifp); fgetpos(ifp,&fpos1);
                  get_var(0,0,CLEAR);
                  for(i=0;i<8;i++)
                    {if(fgets(fstring, 200, ifp)!=NULL)
                       {fstring[36]=0; textcolor(WHITE); textbackground(BLACK); gotoxy(1,8+i); cprintf("%s",fstring);}
                    }
                  fgetpos(ifp,&fpos2); fsetpos(ifp, &fpos1);
                  window(1,1,80,25);
                  message("Program Start");
                  goloop=OFF;
                 }
              }
            puttext(22,19,59,24,buffer);
            break;
         case 18+1000: //alt e
         case 'e':
         case 'E':
            gettext(22,19,59,24,buffer);
            box(22,19,59,24, BLUE,BLACK,WHITE,WHITE);
            if(strcmp(ctrl.file,"NONE")==0)
              {gotoxy(4,3); cprintf("        No Program Loaded");
               gotoxy(4,4); cprintf("            <Any Key>");
              }
            else
              {gotoxy(4,3);
               if(kp==1018) cprintf("      Line By Line Execute?");
               else         cprintf("          Run Program?");
               gotoxy(4,4); cprintf("<Enter> To Accept  <Esc> To Abort");
               textcolor(YELLOW+BLINK);
               gotoxy(1,1); cprintf("<<WARNING!!! MOVEMENT ABOUT TO BEGIN>>");
              }
            window(1,1,80,25);
            while(1)
              {ch=getch();
               if(strcmp(ctrl.file,"NONE")==0) break;
               else if(ch==27 || ch==13 || ch==32) break;
              }
            puttext(22,19,59,24,buffer);
            if(ch==27 || strcmp(ctrl.file,"NONE")==0) break;

            BEGIN_EXEC:
            msg=COMPLETE;
            gettext(41,2,80,2,buffer);
            gotoxy(48,2); cprintf("<<WARNING!!! STAY CLEAR>>");
            gettext(2,6,22,12,buffer4);
            move_menu(3);
            if(ctrl.M00!=ON) {ctrl.M00=OFF; GMTtext(&software, &ctrl, &gcode);}
            message("Program In Progress.");
            if(kp==1018) do_gcode(&hardware, &software, &gcode, &ctrl, &pt, 1, 0); //line by line execute
            else do_gcode(&hardware, &software, &gcode, &ctrl, &pt, 2, 0);  //normal execute
            while(1)
              {ch=fgetc(ifp);
               if(ch=='(') while((ch=fgetc(ifp))!='\n' && ch!=EOF);
               if(ch==EOF) {msg=do_gcode(&hardware, &software, &gcode, &ctrl, &pt, 'M', 30); break;}
               if(ch>=97 && ch<=122) ch=ch-32;
               if(ch=='V' && version!=LITE)
                 {if(NULL==fscanf(ifp, "%lf", &num)) continue;
                  while((ch=fgetc(ifp))==' ' && ch!=EOF);
                  if(ch==EOF) {msg=do_gcode(&hardware, &software, &gcode, &ctrl, &pt, 'M', 30); break;}
                  if(ch=='=')
                    {if(NULL==fscanf(ifp, "%lf", &num2)) continue;
                     get_var(num,num2,STORE);
                    }
                 }
               if(ch=='L' && version!=LITE)
                 {if(goloop==ON || NULL==fscanf(ifp, "%lf", &loopmax)) {continue;}
                  goloop=ON;
                  loopnum=0;
                 }

               if(ch=='X' || ch=='Y' || ch=='Z' || ch==software.a_label || ch=='I' || ch=='J' || ch=='K' ||
                  ch=='R' || ch=='Q' || ch=='F' || ch=='G' || ch=='M' || ch=='P' || ch=='\n')
                 {if(ch=='\n')
                    {num=NULL;
                     if(goloop==ON) {loopnum++; if(loopnum>=abs(loopmax)) goloop=OFF; else fsetpos(ifp,&fpos1);}
                    }
                  else
                    {fgetpos(ifp,&fpostemp);
                     if(NULL==fscanf(ifp, "%lf", &num))
                       {fsetpos(ifp,&fpostemp);
                        while((ch2=fgetc(ifp))==' ' && ch2!=EOF);
                        if(ch2==EOF) {msg=do_gcode(&hardware, &software, &gcode, &ctrl, &pt, 'M', 30); break;}
                        if((ch2=='V' || ch2=='v') && version!=LITE)
                          {if(NULL==fscanf(ifp, "%lf", &num2)) continue;
                           num=get_var(num2,0,GET);
                          }
                        else continue;
                       }
                    }
                  //check for go to subroutine command
                  if(ch=='G' && num==22 && version!=LITE)
                    {while((ch2=fgetc(ifp))==' ' && ch2!=EOF);
                     if(ch2==EOF) {msg=do_gcode(&hardware, &software, &gcode, &ctrl, &pt, 'M', 30); break;}
                     if(ch2=='P' || ch2=='p')
                       {if(NULL==fscanf(ifp, "%lf", &num2)) continue;
                        if(num2<0) continue;
                        for(i=0;i<100;i++) if(sub[i].id==num2) {gosub=ON; subpos=sub[i].pos;}
                       }
                     else continue;
                    }
                  //check for file chain command
                  else if(ch=='G' && num==95)
                    {while((ch2=fgetc(ifp))==' ' && ch2!=EOF);
                     if(ch2=='#' && fscanf(ifp,"%s",tstring)!=NULL && (ifp2=fopen(tstring,"r"))!=NULL)
                       {fclose(ifp);
                        ifp=ifp2;
                        while((ch=fgetc(ifp))!=EOF)
                          {if(ch=='(')
                             {while((ch=fgetc(ifp))!=EOF && ch!='\n');
                              if(ch==EOF) break;
                             }
                           if(ch=='M' || ch=='m')
                             {if(NULL==fscanf(ifp, "%lf", &num)) continue;
                              if(num==30) break;
                             }
                          }
                        //clear old subs from memory and get new subs id's and file positions
                        for(i=0;i<100;i++) sub[i].id=-1;
                        while(ch!=EOF && (ch=fgetc(ifp))!=EOF)
                          {if(ch=='(')
                             {while((ch=fgetc(ifp))!=EOF && ch!='\n');
                              if(ch==EOF) break;
                             }
                           if(ch=='$')
                             {if(NULL==fscanf(ifp, "%lf", &num)) continue;
                              if(num>=0) for(i=0;i<100;i++) if(sub[i].id==-1) {sub[i].id=num; fgetpos(ifp,&sub[i].pos); break;}
                             }
                          }
                          msg=do_gcode(&hardware, &software, &gcode, &ctrl, &world, 'M', 99);
                       }
                    }
                  else msg=do_gcode(&hardware, &software, &gcode, &ctrl, &world, ch, num);
                  //go to subroutine now if needed
                  if(ch=='\n' && gosub==ON)
                    {gosub=OFF;
                     fgetpos(ifp,&mainpos);
                     fsetpos(ifp,&subpos);

                     //do subroutine now
                     while(1)
                       {ch=fgetc(ifp);
                        if(ch=='(') while((ch=fgetc(ifp))!='\n' && ch!=EOF);
                        if(ch==EOF) break;
                        if(ch>=97 && ch<=122) ch=ch-32;
                        if(ch=='V')
                          {if(NULL==fscanf(ifp, "%lf", &num)) continue;
                           while((ch=fgetc(ifp))==' ' && ch!=EOF);
                           if(ch==EOF) {msg=do_gcode(&hardware, &software, &gcode, &ctrl, &pt, 'M', 30); break;}
                           if(ch=='=')
                             {if(NULL==fscanf(ifp, "%lf", &num2)) continue;
                              get_var(num,num2,STORE);
                             }
                          }
                        if(ch=='X' || ch=='Y' || ch=='Z' || ch==software.a_label || ch=='I' || ch=='J' || ch=='K' ||
                           ch=='R' || ch=='Q' || ch=='F' || ch=='G' || ch=='M' || ch=='P' || ch=='\n')
                          {if(ch=='\n') num=NULL;
                           else
                             {fgetpos(ifp,&fpostemp);
                              if(NULL==fscanf(ifp, "%lf", &num))
                                {fsetpos(ifp,&fpostemp);
                                 while((ch2=fgetc(ifp))==' ' && ch2!=EOF);
                                 if(ch2==EOF) {msg=do_gcode(&hardware, &software, &gcode, &ctrl, &pt, 'M', 30); break;}
                                 if(ch2=='V' || ch2=='v')
                                   {if(NULL==fscanf(ifp, "%lf", &num2)) continue;
                                    num=get_var(num2,0,GET);
                                   }
                                 else continue;
                                }
                             }
                           if(ch=='M' && num==2) break;
                           msg=do_gcode(&hardware, &software, &gcode, &ctrl, &world, ch, num);
                          }
                        if(msg==M30 || msg==M99 || msg==M0 || msg==QUIT || msg==E_STOP || (msg>=X_PLIMIT && msg<=A_NLIMIT)) break;
                       }
                     //end subroutine

                     fsetpos(ifp,&mainpos);
                     ch='\n';
                    }
                 }
               if(ch=='\n' && goloop==OFF)
                 {gettext(43,5,78,17,buffer2); puttext(43,4,78,16,buffer2); puttext(43,17,78,17,buffer3);
                  fgetpos(ifp,&fpos1); fsetpos(ifp,&fpos2);
                  if(fgets(fstring, 200, ifp)!=NULL)
                    {fstring[36]=0; gotoxy(43,17); textcolor(WHITE); textbackground(BLACK); cprintf("%s",fstring);}
                  fgetpos(ifp,&fpos2); fsetpos(ifp,&fpos1);

                  if(kp==1018 && msg!=M30 && msg!=M0 && msg!=QUIT && msg!=E_STOP && (msg<X_PLIMIT || msg>A_NLIMIT))
                    {update_display(&software, 0, 0, 0, 0, 0, 0, 0, 3);
                     update_display(&software, 0, 0, 0, 0, 0, 0, 0, 5);
                     if(ctrl.pty==REL)
                        for(i=0;i<4;i++)
                          {if(i==3) i++;
                           update_display(&software, 0, 0, 0, 0, 0, 0, 0, i);
                          }
                     puttext(2,6,22,12,buffer4);
                     gettext(22,19,59,24,buffer5);
                     box(22,19,59,24, RED,BLACK,WHITE,WHITE);
                     gotoxy(4,3); cprintf("           Continue?");
                     gotoxy(4,4); cprintf("<Enter> To Accept  <Esc> To Abort");
                     window(1,1,80,25);
                     while(1)
                       {ch=getch();
                        if(ch==27 || ch==13 || ch==32) break;
                       }
                     puttext(22,19,59,24,buffer5);
                     move_menu(3);
                     if(ch==27) msg=do_gcode(&hardware, &software, &gcode, &ctrl, &pt, 'M', 0);
                    }
                 }
               if(msg==M30 || msg==M99 || msg==M0 || msg==QUIT || msg==E_STOP ||(msg>=X_PLIMIT && msg<=A_NLIMIT)) break;
              }
            if(msg==M30 || msg==M99)
              {window(43,3,78,18); textcolor(WHITE); textbackground(BLACK); clrscr();
               rewind(ifp); fgetpos(ifp,&fpos1);
               get_var(0,0,CLEAR);
               for(i=0;i<8;i++)
                 {if(fgets(fstring, 200, ifp)!=NULL)
                    {fstring[36]=0; textcolor(WHITE); textbackground(BLACK); gotoxy(1,8+i); cprintf("%s",fstring);}
                 }
               fgetpos(ifp,&fpos2); fsetpos(ifp, &fpos1);
               window(1,1,80,25);
               message("Program Start");
              }
            if(msg==M0) {ctrl.M00=ON; message("Program Paused...<E>/<Alt-E> To Cont.");}
            else goloop=OFF;
            puttext(2,6,22,12,buffer4);
            puttext(41,2,80,2,buffer);
            if(msg==M99) goto BEGIN_EXEC;
            break;
         case 'a':
         case 'A':
            gettext(22,18,59,25,buffer);
            box(22,18,59,25, BLUE,BLACK,WHITE,WHITE);
            gotoxy(12,1); cprintf("<Esc> To Exit");
            gotoxy(4,3); cprintf("<1> +90 About %c",software.x_label);
            gotoxy(21,3); cprintf("<2> -90 About %c",software.x_label);
            gotoxy(4,4); cprintf("<3> +90 About %c",software.y_label);
            gotoxy(21,4); cprintf("<4> -90 About %c", software.y_label);
            gotoxy(4,5); cprintf("<5> +90 About %c",software.z_label);
            gotoxy(21,5); cprintf("<6> -90 About %c", software.z_label);
            gotoxy(4,6); cprintf("<7> Switch %c With %c", software.z_label, software.a_label);
            gotoxy(3,8); cprintf("Rotate Coord. System About An Axis");
            while((i=getch())!=27)
              {if(i>='1' && i<='7')
                 {gettext(22,19,59,25,buffer2);
                  box(22,19,59,25, RED,BLACK,WHITE,WHITE);
                  gotoxy(7,3); cprintf("Are You Sure You Want To");
                  gotoxy(6,4);
                  if(i=='1')   cprintf("Rotate +90 Degrees About %c",software.x_label);
                  if(i=='2')   cprintf("Rotate -90 Degrees About %c",software.x_label);
                  if(i=='3')   cprintf("Rotate +90 Degrees About %c",software.y_label);
                  if(i=='4')   cprintf("Rotate -90 Degrees About %c",software.y_label);
                  if(i=='5')   cprintf("Rotate +90 Degrees About %c",software.z_label);
                  if(i=='6')   cprintf("Rotate -90 Degrees About %c",software.z_label);
                  if(i=='7')   cprintf("Switch %c and %c",software.z_label,software.a_label);
                  gotoxy(4,5); cprintf("<Enter> To Accept  <Esc> To Abort");
                  while((ch=getch())!=27 && ch!=13 && ch!=32);
                  if(ch==13 || ch==32)
                    {if(i=='1') rotate_x(&software, &hardware, &pt);
                     if(i=='3') rotate_y(&software, &hardware, &pt);
                     if(i=='5') rotate_z(&software, &hardware, &pt);
                     if(i=='2') {rotate_x(&software, &hardware, &pt); rotate_x(&software, &hardware, &pt); rotate_x(&software, &hardware, &pt);}
                     if(i=='4') {rotate_y(&software, &hardware, &pt); rotate_y(&software, &hardware, &pt); rotate_y(&software, &hardware, &pt);}
                     if(i=='6') {rotate_z(&software, &hardware, &pt); rotate_z(&software, &hardware, &pt); rotate_z(&software, &hardware, &pt);}
                     if(i=='7') switch_z_a(&software, &hardware, &pt);
                     update_display(&software, pt.x_pos, pt.y_pos, pt.z_pos, pt.a_pos, 0, 0, ctrl.frate, ALL);
                    }
                  puttext(22,19,59,25,buffer2);
                 }
              }
            puttext(22,18,59,25,buffer);
            break;
case_27:
;
         case 27:
            gettext(22,19,59,24,buffer);
            box(22,19,59,24, RED,BLACK,WHITE,WHITE);
            gotoxy(4,3); cprintf(" Are You Sure You Want To Exit?");
            gotoxy(4,4); cprintf("<Enter> To Accept  <Esc> To Abort");
            while(1)
              {ch=getch();
               if(ch==27 || ch==13 || ch==32) break;
              }
            if(ch==27) kp=0;
 //           else if(version==DEMO) msg=QUIT;
				else if (msg==MAYBEQUIT) msg=QUIT;
            puttext(22,19,59,24,buffer);
            break;
        }
      switch(kp)
        {case 31+1000:  //alt s
         case 33+1000:  //alt f
         case 24+1000:  //alt o
         case 71+1000:  //home
         case 18+1000:  //alt e
         case 'e': case 'E':
         case 'u': case 'U':
           pt.x_pos=(world.x_pos-gcode.x_coord[ctrl.cs])*ctrl.pty;
           pt.y_pos=(world.y_pos-gcode.y_coord[ctrl.cs])*ctrl.pty;
           pt.z_pos=(world.z_pos-gcode.z_coord[ctrl.cs])*ctrl.pty;
           pt.a_pos=(world.a_pos-gcode.a_coord[ctrl.cs])*ctrl.pty;
           break;
         case 'o': case 'O':
         case 'r': case 'R':
         case 't': case 'T':
         case 72+1000:  //up
         case 77+1000:  //right
         case 80+1000:  //down
         case 75+1000:  //left
         case 73+1000:  //page up
         case 81+1000:  //page down
         case '+': case '=':
         case '-':
         case 'z': case 'Z':
         case 'M': case 'm':
           world.x_pos=world.x_pos-(world.x_pos-gcode.x_coord[ctrl.cs])*ctrl.pty+pt.x_pos;
           world.y_pos=world.y_pos-(world.y_pos-gcode.y_coord[ctrl.cs])*ctrl.pty+pt.y_pos;
           world.z_pos=world.z_pos-(world.z_pos-gcode.z_coord[ctrl.cs])*ctrl.pty+pt.z_pos;
           world.a_pos=world.a_pos-(world.a_pos-gcode.a_coord[ctrl.cs])*ctrl.pty+pt.a_pos;
           pt.x_pos=pt.x_pos*ctrl.pty; pt.y_pos=pt.y_pos*ctrl.pty;
           pt.z_pos=pt.z_pos*ctrl.pty; pt.a_pos=pt.a_pos*ctrl.pty;
           break;
        }
      if(msg>=X_PLIMIT && msg<=A_NLIMIT) msg=limit_msg(&hardware, &software, &pt, msg);
      if(msg==E_STOP)
        {if(ctrl.M03==ON)
           {if(hardware.io1_on==HIGH) clear_io_bit(&hardware, hardware.io1_bit);
            else set_io_bit(&hardware, hardware.io1_bit);
            ctrl.M03=OFF;}
         if(ctrl.M07==ON)
           {if(hardware.io2_on==HIGH) clear_io_bit(&hardware, hardware.io2_bit);
            else set_io_bit(&hardware, hardware.io2_bit);
            ctrl.M07=OFF;}
         if(ctrl.M08==ON)
           {if(hardware.io3_on==HIGH) clear_io_bit(&hardware, hardware.io3_bit);
            else set_io_bit(&hardware, hardware.io3_bit);
            ctrl.M08=OFF;}
         if(ctrl.M10==ON)
           {if(hardware.io4_on==HIGH) clear_io_bit(&hardware, hardware.io4_bit);
            else set_io_bit(&hardware, hardware.io4_bit);
            ctrl.M10=OFF;}
         estop_msg();
        }
      if(msg==BREAK) message("Machine Break Commanded.");
      if(msg==QUIT) break;
      control_text(&software,&pt,&ctrl,&gcode);
      msg=COMPLETE;
     }
		end_realtime();

   //end realtime control

   normvideo();
   _setcursortype(_NORMALCURSOR);
   window(1,1,80,25);
   clrscr();
   if(strcmp(ctrl.file,"NONE")!=0) fclose(ifp);
   save_setup(&hardware, &software, "SETUP.INI");
   save_screen_setup(&world,&pt,&ctrl);

   return 0;
}

void control_screen(struct soft_setup *software, struct pos *pt,
                    struct control *ctrl, struct gcode_setup *gcode)
{
   int i;

   window(1,1,80,25); textbackground(BLACK); clrscr();
   window(24,6,40,14); textbackground(RED); clrscr();

   window(1,1,40,1); textbackground(BLUE); clrscr();
   gotoxy(2,1); textcolor(LIGHTGRAY);
   cprintf("Alt    ile    etup    perator    elp");
   textcolor(WHITE); gotoxy(8,1); putch('F'); gotoxy(15,1); putch('S');
   gotoxy(23,1); putch('O'); gotoxy(34,1); putch('H');

   window(1,1,80,25); textbackground(BLACK); textcolor(WHITE);
      _setcursortype(_NOCURSOR);

   gotoxy(24, 15);   putch(218);
   for(i=25; i<40; i++) putch(196);
   putch(191);
   for(i=16; i<19; i++)
     {gotoxy(24, i);  putch(179);
      gotoxy(40, i);  putch(179);
     }
   gotoxy(24, 19); putch(192);
   for (i=25; i<40; i++) putch(196);
   putch(217);

   gotoxy(1, 2);   putch(218);
   for(i=2; i<40; i++) putch(196);
   putch(191);
   for(i=3; i<4; i++)
     {gotoxy(1, i);  putch(179);
      gotoxy(40, i);  putch(179);
     }
   gotoxy(1, 4); putch(192);
   for (i=2; i<40; i++) putch(196);
   putch(217);

   gotoxy(41, 2);   putch(218);
   for(i=42; i<80; i++) putch(196);
   putch(191);
   for(i=3; i<19; i++)
     {gotoxy(41, i);  putch(179);
      gotoxy(80, i);  putch(179);
     }
   gotoxy(41, 19); putch(192);
   for (i=42; i<80; i++) putch(196);
   putch(217);

   textbackground(RED);
   gotoxy(24, 6);   putch(218);
   for(i=25; i<40; i++) putch(196);
   putch(191);
   for(i=7; i<14; i++)
     {gotoxy(24, i);  putch(179);
      gotoxy(40, i);  putch(179);
     }
   gotoxy(24, 14); putch(192);
   for (i=25; i<40; i++) putch(196);
   putch(217);
   textbackground(BLACK); textcolor(LIGHTGRAY);

   gotoxy(2,7);  cprintf("< >......");
   gotoxy(2,8);  cprintf("< >......");
   gotoxy(2,9);  cprintf("< >......");
   gotoxy(2,10); cprintf("< >......");
   gotoxy(2,11); cprintf("<PgUp>...");
   gotoxy(2,12); cprintf("<PgDn>...");
   gotoxy(2,13); cprintf("<+>......");
   gotoxy(2,14); cprintf("<->..... ");
   gotoxy(2,15); cprintf("<J,Alt-J>");
   gotoxy(2,16); cprintf("<I>......");
   gotoxy(2,17); cprintf("<F>......");
   gotoxy(2,18); cprintf("<Home>...");
   gotoxy(2,19); cprintf("<Z>......");
   gotoxy(2,20); cprintf("<E>......");
   gotoxy(2,21); cprintf("<Alt-E>..");
   gotoxy(2,22); cprintf("<P>......");
   gotoxy(2,23); cprintf("<M>......");
   gotoxy(2,24); cprintf("<A>......");
   gotoxy(24,20); cprintf("<V>...");
   gotoxy(24,21); cprintf("<F5>..");
   gotoxy(24,22); cprintf("<F6>..");
   gotoxy(24,23); cprintf("<F7>..");
   gotoxy(24,24); cprintf("<F8>..");
   gotoxy(54,20); cprintf("<O>..");
   gotoxy(54,21); cprintf("<T>.....");
   gotoxy(54,22); cprintf("<Alt-R>.");
   gotoxy(54,23); cprintf("<U>.....");
   gotoxy(54,24); cprintf("<W>..... ");

     textbackground(BLACK); textcolor(WHITE);

   gotoxy(3,11); cprintf("PgUp");
   gotoxy(3,12); cprintf("PgDn");
   gotoxy(3,13); cprintf("+");
   gotoxy(3,14); cprintf("-");
   gotoxy(3,15); cprintf("J,Alt-J");
   gotoxy(3,16); cprintf("I");
   gotoxy(3,17); cprintf("F");
   gotoxy(3,18); cprintf("Home");
   gotoxy(3,19); cprintf("Z");
   gotoxy(3,20); cprintf("E");
   gotoxy(3,21); cprintf("Alt-E");
   gotoxy(3,22); cprintf("P");
   gotoxy(3,23); cprintf("M");
   gotoxy(3,24); cprintf("A");
   gotoxy(25,20); cprintf("V");
   gotoxy(25,21); cprintf("F5");
   gotoxy(25,22); cprintf("F6");
   gotoxy(25,23); cprintf("F7");
   gotoxy(25,24); cprintf("F8");
   gotoxy(55,20); cprintf("O");
   gotoxy(55,21); cprintf("T");
   gotoxy(55,22); cprintf("Alt-R");
   gotoxy(55,23); cprintf("U");
   gotoxy(55,24); cprintf("W");

   gotoxy(42,10); putch(26);


   gotoxy(12,7); cprintf("Jog X+");
   gotoxy(12,8); cprintf("Jog X-");
   gotoxy(12,9); cprintf("Jog Y+");
   gotoxy(12,10); cprintf("Jog Y-");
   gotoxy(12,11); cprintf("Jog Z+");
   gotoxy(12,12); cprintf("Jog Z-");
   gotoxy(12,15); cprintf("Jog");
   gotoxy(12,16); cprintf("Inc");
   gotoxy(12,17); cprintf("Feed Rate");
   gotoxy(12,18); cprintf("Home");
   gotoxy(12,19); cprintf("Adj. Axis");
   gotoxy(12,20); cprintf("Execute");
   gotoxy(12,21); cprintf("Line Exe.");
   gotoxy(12,22); cprintf("Program");
   gotoxy(12,23); cprintf("Move");
   gotoxy(12,24); cprintf("Rot/SW Ax");
   gotoxy(31,20); cprintf("ConstantContour");
   gotoxy(60,20); cprintf("Coordinate Offset");
   gotoxy(63,21); cprintf("Tool");
   gotoxy(63,22); cprintf("Move");
   gotoxy(63,23); cprintf("Units");


   control_text(software, pt, ctrl, gcode);
}

void control_text(struct soft_setup *software, struct pos *pt,
                  struct control *ctrl, struct gcode_setup *gcode)
{
   window(1,1,80,25); textbackground(BLACK); textcolor(WHITE);
   update_display(software, (*pt).x_pos, (*pt).y_pos, (*pt).z_pos, (*pt).a_pos, 0, 0, (*ctrl).frate, ALL);


//   gotoxy(2,7);  cprintf("<%c>.......",26);   right arrow
//   gotoxy(2,8);  cprintf("<%c>.......",27);   left arrow
//   gotoxy(2,9);  cprintf("<%c>.......",24);
//   gotoxy(2,10); cprintf("<%c>.......",25);   down

   if((*software).invert_arrows==0 ||(*software).invert_arrows==1 ){ gotoxy(3,7);  cprintf("%c",26);}
   if((*software).invert_arrows==2 ||(*software).invert_arrows==3 ){ gotoxy(3,7);  cprintf("%c",27);}
   if((*software).invert_arrows==5 ||(*software).invert_arrows==7 ){ gotoxy(3,7);  cprintf("%c",24);}
   if((*software).invert_arrows==4 ||(*software).invert_arrows==6 ){ gotoxy(3,7);  cprintf("%c",25);}

   if((*software).invert_arrows==2 ||(*software).invert_arrows==3 ){ gotoxy(3,8);  cprintf("%c",26);}
   if((*software).invert_arrows==0 ||(*software).invert_arrows==1 ){ gotoxy(3,8);  cprintf("%c",27);}
   if((*software).invert_arrows==4 ||(*software).invert_arrows==6 ){ gotoxy(3,8);  cprintf("%c",24);}
   if((*software).invert_arrows==5 ||(*software).invert_arrows==7 ){ gotoxy(3,8);  cprintf("%c",25);}

   if((*software).invert_arrows==4 ||(*software).invert_arrows==5 ){ gotoxy(3,9);  cprintf("%c",26);}
   if((*software).invert_arrows==6 ||(*software).invert_arrows==7 ){ gotoxy(3,9);  cprintf("%c",27);}
   if((*software).invert_arrows==1 ||(*software).invert_arrows==3 ){ gotoxy(3,9);  cprintf("%c",24);}
   if((*software).invert_arrows==0 ||(*software).invert_arrows==2 ){ gotoxy(3,9);  cprintf("%c",25);}

   if((*software).invert_arrows==6 ||(*software).invert_arrows==7 ){ gotoxy(3,10);  cprintf("%c",26);}
   if((*software).invert_arrows==4 ||(*software).invert_arrows==5 ){ gotoxy(3,10);  cprintf("%c",27);}
   if((*software).invert_arrows==0 ||(*software).invert_arrows==2 ){ gotoxy(3,10);  cprintf("%c",24);}
   if((*software).invert_arrows==1 ||(*software).invert_arrows==3 ){ gotoxy(3,10);  cprintf("%c",25);}

   gotoxy(12,13);
   if((*software).a_status==ON) cprintf("Jog %c+",(*software).a_label);
   else cprintf("<N/A> ");
   gotoxy(12,14);
   if((*software).a_status==ON) cprintf("Jog %c-",(*software).a_label);
   else cprintf("<N/A> ");
   gotoxy(15,15);
   if((*ctrl).jog_type==INC) cprintf(" (Inc) ");
   if((*ctrl).jog_type==SLOW) cprintf(" (Slow)");
   if((*ctrl).jog_type==FAST) cprintf(" (Fast)");
   gotoxy(15,16); cprintf("(%7s)",prn_val((*ctrl).jog_inc));
   gotoxy(31,21); cprintf("%s",(*software).io1_label);
   gotoxy(31,22); cprintf("%s",(*software).io2_label);
   gotoxy(31,23); cprintf("%s",(*software).io3_label);
   gotoxy(31,24); cprintf("%s",(*software).io4_label);
   gotoxy(26,16); cprintf("   FR");
   gotoxy(26,17); cprintf("CurFR");
   gotoxy(26,18); cprintf(" %c FR",(*software).a_label);
   gotoxy(48,1); cprintf("G-Code File: %12s",(*ctrl).file);

   gotoxy(2,3); cprintf("Machine Configuration: %12s", (*software).file);

   if((*software).a_status==OFF) {gotoxy(32,18); cprintf("<N/A>  ");}
   textbackground(RED);
   if((*software).a_status==OFF) {gotoxy(29,13); cprintf("<N/A>     ");}
   gotoxy(27,7); cprintf("X");
   gotoxy(27,9); cprintf("Y");
   gotoxy(27,11); cprintf("Z");
   gotoxy(27,13); cprintf("%c",(*software).a_label);
   GMTtext(software,ctrl,gcode);
}

void GMTtext(struct soft_setup *software, struct control *ctrl, struct gcode_setup *gcode)
{
   window(1,1,80,25);
   textcolor(WHITE); textbackground(BLACK);
//   if(version==LITE) {gotoxy(44,20); cprintf("(N/A)");}
//   else if((*ctrl).cvv==ON) {gotoxy(47,20); cprintf("(ON) ");}
//   else {gotoxy(47,20); cprintf("(OFF)");}
//   if(version==LITE) {gotoxy(44,20); cprintf("(N/A)");}
   if((*ctrl).cvv==ON) {gotoxy(47,20); cprintf("(ON) ");}
   else {gotoxy(47,20); cprintf("(OFF)");}
   if((*ctrl).M03==ON) {gotoxy(36,21); cprintf("(ON) ");}
   else {gotoxy(36,21); cprintf("(OFF)");}
   if((*ctrl).M07==ON) {gotoxy(36,22); cprintf("(ON) ");}
   else {gotoxy(36,22); cprintf("(OFF)");}
   if((*ctrl).M08==ON) {gotoxy(36,23); cprintf("(ON) ");}
   else {gotoxy(36,23); cprintf("(OFF)");}
   if((*ctrl).M10==ON) {gotoxy(36,24); cprintf("(ON) ");}
   else {gotoxy(36,24); cprintf("(OFF)");}
   if((*ctrl).cs==0) {gotoxy(77,20); cprintf("(M)");}
   if((*ctrl).cs==1) {gotoxy(77,20); cprintf("(2)");}
   if((*ctrl).cs==2) {gotoxy(77,20); cprintf("(3)");}
   if((*ctrl).cs==3) {gotoxy(77,20); cprintf("(4)");}
   if((*ctrl).cs==4) {gotoxy(77,20); cprintf("(5)");}
   if((*ctrl).cs==5) {gotoxy(77,20); cprintf("(6)");}
   if((*ctrl).cs==6) {gotoxy(77,20); cprintf("(7)");}
   gotoxy(69,21); cprintf("(%2d)",(*gcode).tnum);
   if((*ctrl).pty==ABS) {gotoxy(69,22); cprintf("(Abs)");}
   else {gotoxy(69,22); cprintf("(Rel)");}
   if((*software).units==MM) {gotoxy(69,23); cprintf("(MM)");}
   else {gotoxy(69,23); cprintf("(IN)");}
   if((*software).a_status==ON)
     {if((*software).a_follow==INDEPENDANT) {gotoxy(63,24); cprintf("%c (Independent)",(*software).a_label);}
      if((*software).a_follow==FOLLOW_X) {gotoxy(63,24);    cprintf("%c (Follow X)   ",(*software).a_label);}
      if((*software).a_follow==FOLLOW_Y) {gotoxy(63,24);    cprintf("%c (Follow Y)   ",(*software).a_label);}
      if((*software).a_follow==FOLLOW_Z) {gotoxy(63,24);    cprintf("%c (Follow Z)   ",(*software).a_label);}
     }
//   else {gotoxy(62,24); cprintf("%c(N/A)         ",(*software).a_label);}
      else {gotoxy(63,24); cprintf(" (N/A)         ",(*software).a_label);}

   }

int limit_msg(struct hard_setup *hardware, struct soft_setup *software, struct pos *pt, int msg)
{
   int xp=1, xn=1, yp=1, yn=1, zp=1, zn=1, ap=1, an=1, ch, temp_a_follow=(*software).a_follow;
   static BYTE buffer[1000];
   char letter;

   gettext(22,19,59,24,buffer);
   box(22,19,59,24, RED,BLACK,WHITE,WHITE);
   gotoxy(8,3); cprintf("All Processes Aborted");
   gotoxy(5,4); cprintf("Use Jog Keys To Clear Limits");
   if(msg==X_PLIMIT) {xp=0; letter='X';} if(msg==X_NLIMIT) {xn=0; letter='X';} if(msg==Y_PLIMIT) {yp=0; letter='Y';} if(msg==Y_NLIMIT) {yn=0; letter='Y';}
   if(msg==Z_PLIMIT) {zp=0; letter='Z';} if(msg==Z_NLIMIT) {zn=0; letter='Z';} if(msg==A_PLIMIT) {ap=0; letter=(*software).a_label;} if(msg==A_NLIMIT) {an=0; letter=(*software).a_label;}
   textcolor(YELLOW+BLINK); gotoxy(8,1); cprintf("<<%c Axis Limit Override>>",letter);
   (*software).a_follow=INDEPENDANT;
   while(xp==0 || xn==0 || yn==0 || yp==0 || zn==0 || zp==0 || an==0 || ap==0)
     {ch=getch(); if(ch==0) ch=getch()+1000;
      if(ch==1014) break;
      switch(ch)
        {
         case 75+1000:     //left
               if((*software).invert_arrows==0 || (*software).invert_arrows==1 )
                 {if((msg=jog(hardware, software, pt, SLOW, NULL, X_AXIS*xn, NEG))==UNLIMIT) xp=1;}
         else
               if((*software).invert_arrows==2 || (*software).invert_arrows==3 )
                 {if((msg=jog(hardware, software, pt, SLOW, NULL, X_AXIS*xp, POS))==UNLIMIT) xn=1;}
         else
               if((*software).invert_arrows==4 || (*software).invert_arrows==5 )
                 {if((msg=jog(hardware, software, pt, SLOW, NULL, Y_AXIS*yn, NEG))==UNLIMIT) yp=1;}
         else
               if((*software).invert_arrows==6 || (*software).invert_arrows==7 )
                 {if((msg=jog(hardware, software, pt, SLOW, NULL, Y_AXIS*yp, POS))==UNLIMIT) yn=1;}
         break;

         case 77+1000:     //right
               if((*software).invert_arrows==0 || (*software).invert_arrows==1 )
                 {if((msg=jog(hardware, software, pt, SLOW, NULL, X_AXIS*xp, POS))==UNLIMIT) xn=1;}
           else
               if((*software).invert_arrows==2 || (*software).invert_arrows==3 )
                 {if((msg=jog(hardware, software, pt, SLOW, NULL, X_AXIS*xn, NEG))==UNLIMIT) xp=1;}
           else
               if((*software).invert_arrows==4 || (*software).invert_arrows==5 )
                 {if((msg=jog(hardware, software, pt, SLOW, NULL, Y_AXIS*yp, POS))==UNLIMIT) yn=1;}
           else
               if((*software).invert_arrows==6 || (*software).invert_arrows==7 )
                 {if((msg=jog(hardware, software, pt, SLOW, NULL, Y_AXIS*yn, NEG))==UNLIMIT) yp=1;}
           break;

         case 72+1000:     //up
               if((*software).invert_arrows==4 || (*software).invert_arrows==6 )
                 {if((msg=jog(hardware, software, pt, SLOW, NULL, X_AXIS*xn, NEG))==UNLIMIT) xp=1;}
           else
               if((*software).invert_arrows==5 || (*software).invert_arrows==7 )
                 {if((msg=jog(hardware, software, pt, SLOW, NULL, X_AXIS*xp, POS))==UNLIMIT) xn=1;}
           else
               if((*software).invert_arrows==0 || (*software).invert_arrows==2 )
                 {if((msg=jog(hardware, software, pt, SLOW, NULL, Y_AXIS*yn, NEG))==UNLIMIT) yp=1;}
         else
               if((*software).invert_arrows==1 || (*software).invert_arrows==3 )
                 {if((msg=jog(hardware, software, pt, SLOW, NULL, Y_AXIS*yp, POS))==UNLIMIT) yn=1;}
         break;

         case 80+1000:     //down
               if((*software).invert_arrows==4 || (*software).invert_arrows==6 )
               {if((msg=jog(hardware, software, pt, SLOW, NULL, X_AXIS*xp, POS))==UNLIMIT) xn=1;}
           else
               if((*software).invert_arrows==5 || (*software).invert_arrows==7 )
                 {if((msg=jog(hardware, software, pt, SLOW, NULL, X_AXIS*xn, NEG))==UNLIMIT) xp=1;}
           else
               if((*software).invert_arrows==0 || (*software).invert_arrows==2 )
               {if((msg=jog(hardware, software, pt, SLOW, NULL, Y_AXIS*yp, POS))==UNLIMIT) yn=1;}
           else
               if((*software).invert_arrows==1 || (*software).invert_arrows==3 )
               {if((msg=jog(hardware, software, pt, SLOW, NULL, Y_AXIS*yn, NEG))==UNLIMIT) yp=1;}
           break;

			case 73+1000:   //page up
            if((msg=jog(hardware, software, pt, SLOW, NULL, Z_AXIS*zp, POS))==UNLIMIT) zn=1;
            break;

         case 81+1000:   //page down
            if((msg=jog(hardware, software, pt, SLOW, NULL, Z_AXIS*zn, NEG))==UNLIMIT) zp=1;
            break;

         case '+':
         case '=':
            if((msg=jog(hardware, software, pt, SLOW, NULL, A_AXIS*ap, POS))==UNLIMIT) an=1;
            break;

         case '-':
            if((msg=jog(hardware, software, pt, SLOW, NULL, A_AXIS*an, NEG))==UNLIMIT) ap=1;
            break;
        }
      if(msg==E_STOP || msg==QUIT) break;
      if(msg==X_PLIMIT) xp=0; if(msg==X_NLIMIT) xn=0; if(msg==Y_PLIMIT) yp=0; if(msg==Y_NLIMIT) yn=0;
      if(msg==Z_PLIMIT) zp=0; if(msg==Z_NLIMIT) zn=0; if(msg==A_PLIMIT) ap=0; if(msg==A_NLIMIT) an=0;
      msg=COMPLETE;
     }
   (*software).a_follow=temp_a_follow;
   puttext(22,19,59,24,buffer);
   return msg;
}

void estop_msg(void)
{
   static BYTE buffer[1000];
   gettext(22,19,59,24,buffer);
   box(22,19,59,24, RED,BLACK,WHITE,WHITE);
   gotoxy(10,3); cprintf("All Processes Aborted");
   gotoxy(16,4); cprintf("<Any Key>");
   textcolor(YELLOW+BLINK); gotoxy(11,1); cprintf("<<Emergeny Stop>>");
   getch();
   puttext(22,19,59,24,buffer);
}

void message(char msg[40])
{
   textcolor(WHITE);
   textbackground(BLACK);
   window(1,1,80,25);
   gotoxy(2,3); cprintf("                                      ");
   gotoxy(2,3); cprintf("%s",msg);
}

void move_menu(int id)
{
   int i;

   window(2,6,22,12);
   textbackground(GREEN); textcolor(WHITE); clrscr();
   window(1,1,80,25);
   gotoxy(2, 6);   putch(218);
   for(i=3; i<22; i++) putch(196);
   putch(191);
   for(i=7; i<12; i++)
     {gotoxy(2, i);  putch(179);
      gotoxy(22, i);  putch(179);
     }
   gotoxy(2, 12); putch(192);
   for (i=3; i<22; i++) putch(196);
   putch(217);
                       gotoxy(3,7); cprintf("   Move Control");
                       gotoxy(3,8); cprintf("<Esc>/<Sp>...Break");
   if(id==2 || id==3) {gotoxy(3,9); cprintf("<F>.....+Feed Rate");
                       gotoxy(3,10); cprintf("<S>.....-Feed Rate");}
   if(id==3)          {gotoxy(3,11); cprintf("<P>..........Pause");}
}


double get_var(double num, double num2, int action)
{
   static double V[101];
   int i;

   if(num>100 || num<0) return 0;
   if(action==CLEAR) for(i=0;i<101;i++) V[i]=0;
   if(action==STORE) V[num]=num2;
   if(action==GET) return V[num];
   return 1;
}

